package com.example.mydataaccesssample;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ActFile extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.common);
		InitialComponent();
	}
	OnClickListener btnSave_click=new OnClickListener(){
		public void onClick(View arg0) {
			
			try {
				OutputStream l_outStream;
				//先開啟這個檔案
				l_outStream = openFileOutput("myfile.txt",Activity.MODE_PRIVATE);
				//寫入這個檔案，用UTF8
				l_outStream.write(txtSave.getText().toString()
						.getBytes("utf-8"));
				//關閉
				l_outStream.close();
		        
				Toast.makeText(ActFile.this, "儲存資料成功", Toast.LENGTH_SHORT).show();
		        
		        
			} catch (FileNotFoundException e) {
				
				
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	}};

	OnClickListener btnGet_click=new OnClickListener(){
		public void onClick(View arg0) {
			try {
				String l_str="";		        
				InputStream l_inStream=openFileInput("myfile.txt");
				byte[] l_bytes=new byte[100];
				int l_intCount=l_inStream.read(l_bytes);
				l_str=new String(l_bytes,0,l_intCount,"utf-8");
				l_inStream.close();
				lblGet.setText(l_str);
		        
			} catch (FileNotFoundException e) {
				
				
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

	}};
	
	private void InitialComponent() {
		
		btnSave=(Button)findViewById(R.id.btnSave);
		btnSave.setOnClickListener(btnSave_click);
		
		btnGet=(Button)findViewById(R.id.btnGet);
		btnGet.setOnClickListener(btnGet_click);
		
		lblGet=(TextView)findViewById(R.id.lblGet);
		txtSave=(EditText)findViewById(R.id.txtSave);
	
	}
   
	Button btnSave=null;
	Button btnGet=null;
	TextView lblGet=null;
	EditText txtSave=null;


}
